package com.learn.springbootnetty.bytebufferdemo;

import java.nio.ByteBuffer;

/**
 * @author xiaodonghong
 * @description TODO
 * @date 2023/6/15-17:04
 */
public class ByteBufferDemo {

    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024*1024*10);
        for (int i = 0; i < 1024*1024*1024*900; i++) {
            byteBuffer.put((byte) i);
        }
        ByteBuffer allocatedDirect = ByteBuffer.allocateDirect(1024*1024*10);
        for (int i = 0; i < 1024*1024*1024*900; i++) {
            allocatedDirect.put((byte) i);
        }
        System.out.println();
    }

    public static void main2() {
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        for (int i = 0; i < 14; i++) {
            byteBuffer.put((byte) i);
        }
        //
        byteBuffer.flip();
        byteBuffer.get();
        byteBuffer.get();
        while (byteBuffer.hasRemaining()) {
            System.out.println(byteBuffer.get());
        }

        byteBuffer.flip();
        byteBuffer.get();
        byteBuffer.get();
        while (byteBuffer.hasRemaining()) {
            System.out.println(byteBuffer.get());
        }
        byteBuffer.clear();
        for (int i = 0; i < 14; i++) {
            byteBuffer.put((byte) ('i'+i));
        }
        byteBuffer.put(5, (byte) 20);
        byteBuffer.putShort(0,(short) 40);
        byteBuffer.putInt(10,20);
    }
}
